﻿using STMS.BLL;
using STMS.Models.DModels;
using STMS.STMSApp.FModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STMS.STMSApp.DZPDCheck
{
    public partial class FrmDZPDInfo : Form
    {
        public FrmDZPDInfo()
        {
            InitializeComponent();
        }
        DZPDBLL dzpdBLL = new DZPDBLL();
        public event Action ReLoadList;//刷新列表页数据
        public FInfoData fInfo = null;
        int dzpdCheckId = 0;//当前操作的结果信息编号
        int actType = 1;
        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void FrmDZPDInfo_Load(object sender, EventArgs e)
        {
            if(this.Tag != null)
            {
                fInfo = this.Tag as FInfoData;
                InitPageInfo();//初始化电子皮带秤操作信息页面
            }
        }

        private void InitPageInfo()
        {
            if(fInfo != null )
            {
                actType = fInfo.ActType;
                if(actType == 1)//新增加载
                {
                    btnClear.Enabled = true;
                    btnUpload.Text = "确定";
                    this.Text = "电子皮带秤核查信息页面";
                    double mf = Convert.ToDouble(FrmDZPDLoad.MF);
                    double af = Convert.ToDouble(FrmDZPDLoad.AF);
                    double record = (mf - af) * 100 / af;
                    txt_R_Record.Text = record.ToString("#0.0000") + "%";
                    txtAF.Text = FrmDZPDLoad.AF;
                    if (Math.Abs(record) <= 0.5)
                    {
                        txt_Result.Text = "合格";
                    }
                    else
                    {
                        txt_Result.Text = "不合格";
                    }
                }
                else//修改加载
                {
                    dzpdCheckId = fInfo.FId;
                    btnClear.Enabled = false;
                    btnUpload.Text = "修改";
                    this.Text = "电子皮带秤信息修改页面";
                    GetCheckInfo(dzpdCheckId);
                }
            }
        }

        private void GetCheckInfo(int CheckId)
        {
            HistorInfo dzpdInfo = dzpdBLL.GetInfo(CheckId);
            if(dzpdInfo != null)
            {
                txt_R_Record.Text = dzpdInfo.R_RECORD;
                txtAF.Text = dzpdInfo.R_STAND;
                txt_Result.Text = dzpdInfo.R_RESULT;
                txt_R_Person.Text = dzpdInfo.R_PERSON;
            }
        }

        /// <summary>
        /// 操作页面信息清空
        /// </summary>
        private void ClearInfo()
        {
            txt_R_Record.Clear();
            txtAF.Clear();
            txt_Result.Clear();
            txt_R_Person.Clear();

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearInfo();
        }
        /// <summary>
        /// 操作信息上传提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpload_Click(object sender, EventArgs e)
        {
            string conType = actType == 1 ? "上传" : "修改";
            string msgTitle = $"{conType}核查信息";
            //信息输入
            string dzpd_Record = txt_R_Record.Text.Trim();
            string dzpdActualFlow = txtAF.Text.Trim();
            string dzpd_Result = txt_Result.Text.Trim();
            string dzpd_Person = txt_R_Person.Text.Trim();
            int dzpdProjectID = 1;
            if (actType == 1)
            {
                string dzpdStation = FrmDZPDLoad.Station.Trim();
                HistorInfo dzpdInfo = new HistorInfo()
                {
                    R_C_ID = dzpdProjectID,
                    R_STAND= dzpdActualFlow,
                    R_RECORD = dzpd_Record,
                    R_RESULT = dzpd_Result,
                    R_PERSON = dzpd_Person,
                    R_OP = dzpdStation
                };
                int checkNewId = dzpdBLL.AddDZPDInfos(dzpdInfo);
                if(checkNewId > 0)
                {
                    MsgBoxHelper.MsgBoxShow(msgTitle, "核查结果上传成功！");
                    ReLoadList?.Invoke();
                }
                else
                {
                    MsgBoxHelper.MsgBoxShow(msgTitle, "核查结果上传失败！");
                    return;
                }
            }
            else
            {
                HistorInfo dzpdInfo = new HistorInfo()
                {
                    R_ID = dzpdCheckId,
                    R_STAND = dzpdActualFlow,
                    R_RECORD = dzpd_Record,
                    R_RESULT = dzpd_Result,
                    R_PERSON = dzpd_Person
                    
                };
                bool bl = dzpdBLL.UpdateDZPDInfos(dzpdInfo);
                if(bl)
                {
                    MsgBoxHelper.MsgBoxShow(msgTitle, "修改成功！");
                    ReLoadList?.Invoke();
                }
                else
                {
                    MsgBoxHelper.MsgBoxShow(msgTitle, "修改失败！");
                    return;
                }
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            InitPageInfo();
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }
    }
}
